$(function () {
    initDoailog_user();
    /**初始化时间选择**/
    $("#phone_time span").html(moment().subtract("days",29).format("YYYY-MM-DD")+" - "+moment().format("YYYY-MM-DD"));
    $('#phone_time').daterangepicker({
        timePicker: true, //显示时间
        timePicker24Hour: true, //时间制
        timePickerSeconds: true, //时间显示到秒
        startDate: moment().hours(0).minutes(0).seconds(0), //设置开始日期
        endDate: moment(new Date()), //设置结束器日期
        maxDate: moment(new Date()), //设置最大日期
        minDate: moment().add('year',-1), //设置最小日期
        "opens": "center",
        ranges: {
            '今天': [moment(), moment()],'昨天': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
            '上周': [moment().subtract(6, 'days'), moment()],
            '前30天': [moment().subtract(29, 'days'), moment()],
            '本月': [moment().startOf('month'), moment().endOf('month')],
            '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        },
        showWeekNumbers: true,
        locale: {
            format: "YYYY-MM-DD HH:mm:ss", //设置显示格式
            applyLabel: '确定', //确定按钮文本
            cancelLabel: '取消', //取消按钮文本
            customRangeLabel: '自定义',
            daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
            monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
                '七月', '八月', '九月', '十月', '十一月', '十二月'
            ],
            firstDay: 1
        },
    }).on('apply.daterangepicker', function(ev, picker) {
        var starttime = picker.startDate.format('YYYY-MM-DD');
        var endtime = picker.endDate.format('YYYY-MM-DD');
        $("#phone_time span").html(starttime+" - "+endtime);
        $("#starttime").val(starttime);
        $("#endtime").val(endtime);
        $JuXue.refreshTable("saleCustDialogTable");
    });
    var settings = {
        url: ctx + "workAnlynis/getWorkDialogAncilyList",
        pageList: [10,50],
        sidePagination: "client",
        queryParams: function(params) {
            return {
                diaName :  $("#gerenyuanid").val(), //选择的人员
                starttime : $("#starttime").val(), //开始时间
                endtime   : $("#endtime").val()    //结束时间
            };
        },
        columns: [{
            checkbox: true,
        }, {
            field: 'diaName',
            class: 'text-center',
            title: '姓名'
        }, {
            field: 'number',
            class: 'text-center',
            title: '分机号',
        }, {
            field: 'dest',
            class: 'text-center',
            title: '目标电话',
        },{
            field: 'dialogDruid',
            class: 'text-center',
            title: '电话时长',
        },{
            field: 'dialogtime',
            class: 'text-center',
            title: '拨打时间',
        },{
            field: 'dialogUrl',
            class: 'text-center',
            width: '100',
            title: '录音文件',
            formatter:function (value,index,row) {
                return "<audio class='height-30 width-200' src="+value+"  controls=\"controls\" controlsList=\"nodownload\" οncοntextmenu=\"return false\" type=\"audio/wav\"></audio>";
            }
        }]
    };
    $JuXue.initTable('saleCustDialogTable', settings);
});
function initDoailog_user(){
    $.post(ctx+"dept/getUserAndDeptByTreeWhisRole",{},function (r) {
        var array  = r.msg;
        $("#select_label_user").jstree({
            "core": {"multiple": true,"data":array, "icon":false, "themes":{"icons":false}},
            "checkbox" : {"keep_selected_style" : false},
            "plugins" : [ "wholerow", "checkbox" ]
        });
        // 获得点击节点的id
        $('#select_label_user').on("changed.jstree", function (e, data) {
            var nodeid = data.node.id;
            var text = data.node.text;
            var action = data.action;;
            if (action == "deselect_node") {
                $("#otherUserSelect").find("div").each(function () {var data_id = $(this).attr("data-id");if (data_id == nodeid) $(this).remove();});
            }else{
                /**判断是不是超过 5个 超过5个则不能继续追加**/
                var divlength = $("#otherUserSelect").find("div");
                if(divlength.length >= 3){$JuXue.n_danger("选择查询人数不能超过3个");$("#select_label_user").jstree("deselect_node",nodeid);return;
                }else{
                    if(divlength.length >0){
                        var ble = false;$("#otherUserSelect").find("div").each(function () {var data_id = $(this).attr("data-id");if (data_id == nodeid){ble = true;}});
                        if(!ble){var label = "<div class='label label-danger p-3 m-3 ' data-id='"+nodeid+"'>"+text+"</div>";$("#otherUserSelect").append(label);}
                    }else{
                        var label = "<div class='label label-danger p-3 m-3 ' data-id='"+nodeid+"'>"+text+"</div>";
                        $("#otherUserSelect").append(label);
                    }
                }
            }
        });
    });
}
/**选择时间类型**/
function custphoneTime(btn) {
    var data_cliak = $(btn).attr("data-click");
    if(data_cliak === "selft"){
        $("#phone_time").removeClass("hidden");
        $("#starttime").val(moment().subtract("days",29).format("YYYY-MM-DD"));
        $("#endtime").val(moment().format("YYYY-MM-DD"));
    }else if (data_cliak == "7") {
        $("#phone_time").addClass("hidden");
        $("#starttime").val(moment().subtract(6, 'days').format("YYYY-MM-DD"));
        $("#endtime").val(moment().format("YYYY-MM-DD"));
    }else{
        $("#phone_time").addClass("hidden");
        $("#starttime").val(moment().subtract(29, 'days').format("YYYY-MM-DD"));
        $("#endtime").val(moment().format("YYYY-MM-DD"));
    }
    $("#custPhoneTime").find("button").each(function () {$(this).removeClass("btn-primary");});
    $(btn).addClass("btn-primary");
    $JuXue.refreshTable("saleCustDialogTable");
}
/**选择员工**/
function selectUserListque(){
    $('#selectsaleUsers').removeClass('open');
    var allUser = $('#select_label_user').jstree(true).get_selected(false);//获得整个树
    var usersArray = new Array();
    var nicknameArray = new Array();
    if (allUser.length > 0){
        //说明选择了人员
        $("#otherUserSelect").find("div").each(function () {
            var nickname = $(this).text();
            var users = $(this).attr("data-id");
            usersArray.push(users);
            nicknameArray.push(nickname);
        });
        $("#gerenyuanid").val(usersArray.join(","));
        $("#gerenyuanname").val(nicknameArray.join(","));
    }else{
        //没有选择人员
        $("#gerenyuanid").val("");
        $("#gerenyuanname").val("");
        $("#nouserSelect").addClass("btn-primary");
    }
    $JuXue.refreshTable("saleCustDialogTable");
}
/**导出信息**/
function exportDialogStaticInfo(btn){
    $(btn).text("导出中....").removeAttrs("onclick");
    var diaName  =  $("#gerenyuanid").val();//选择的人员
    var starttime =  $("#starttime").val(); //开始时间
    var endtime   =  $("#endtime").val();    //结束时间
    $.post(ctx + 'workAnlynis/downworkDiaklogExcel', { "diaName": diaName,"starttime":starttime,"endtime":endtime }, function(r) {
        if (r.code === 0) {window.location.href = "file/download?fileName=" + r.msg + "&delete=" + true;
        $(btn).text("导出电话联系统计").attr("onclick","exportDialogStaticInfo(this)");
        } else {$JuXue.n_warning("系统错误");}
    });

}


